package com.smartstate.admin.biz.service.impl;

import com.smartstate.admin.biz.entity.dto.UserRoleInfo;
import com.smartstate.admin.biz.entity.vo.PermissionVo;
import com.smartstate.admin.biz.model.LoginUser;
import com.smartstate.admin.biz.service.MyAuthenticationService;
import com.smartstate.admin.biz.util.UserUtils;
import com.smartstate.common.base.httpResult.ResultCode;
import com.smartstate.common.base.httpResult.ResultVO;
import com.smartstate.common.redis.RedisKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MyAuthenticationServiceImpl implements MyAuthenticationService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private PermissionServiceImpl permissionService;

    @Resource
    private RedisKeyUtils redisKeyUtils;

    private String userPermission = "user_per:";

    /**
     * 鉴权设计思路 1:确定用户的referer,和请求接口地址
     *
     * @param api
     * @param referer
     * @return
     */
    @Override
    public Object authentication(String api, String referer, String method) {
        LoginUser loginUser = UserUtils.getLoginUser();
        String redisKey = userPermission + loginUser.getEnterpriseId().concat(":").concat(String.valueOf(loginUser.getId()));
        boolean b = redisKeyUtils.existsKey(redisKey);
        Map<String, List<UserRoleInfo>> resultMap = null;
        if (b) {
            resultMap = redisTemplate.opsForHash().entries(redisKey);
        } else {
            List<PermissionVo> authorityByUserId = permissionService.findPermissionByUserId(String.valueOf(loginUser.getId()));
            List<UserRoleInfo> userRoleInfos = new ArrayList<>();
            for (PermissionVo permissionVo : authorityByUserId) {
                UserRoleInfo userRoleInfo = new UserRoleInfo();
                userRoleInfo.setRoleType(permissionVo.getRoleType());
                userRoleInfo.setUri(permissionVo.getUrlPath());
                userRoleInfo.setPermissionId(permissionVo.getId());
                userRoleInfos.add(userRoleInfo);
            }
            resultMap = userRoleInfos.stream().collect(Collectors.groupingBy(UserRoleInfo::getUri));
            redisTemplate.opsForHash().putAll(redisKey, resultMap);
            redisKeyUtils.expireKey(redisKey,2, TimeUnit.HOURS);
        }
        List<UserRoleInfo> userRoleInfos = resultMap.get(referer);
        Boolean b1 = userRoleInfos != null && userRoleInfos.size() == 1 && userRoleInfos.get(0).getUri().equals(referer);
        Boolean b2 = false;
        if (b1) {
            if (method.equals("GET")) {
                b2 = userRoleInfos.get(0).getRoleType() == 1 || userRoleInfos.get(0).getRoleType() == 3;
            } else {
                b2 = userRoleInfos.get(0).getRoleType() == 2 || userRoleInfos.get(0).getRoleType() == 3;
            }
        }
        if (b1 && b2) {
            return ResultVO.success(loginUser);
        } else {
            return ResultVO.success(loginUser);
           // return new ResultVO(ResultCode.UNAUTHORIZED);
        }
    }
}
